package com.ltf.service;

import com.ltf.dao.*;
import com.ltf.entity.*;
import com.ltf.dao.*;
import com.ltf.entity.*;
import com.ltf.dao.*;
import com.ltf.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//
@Service
public class MarkServiceImpl implements MarkService{
    @Autowired(required = false)
    MarkDao markDao;
    @Autowired(required = false)
    ClzDao clzDao;
    @Autowired(required = false)
    CourseDao courseDao;
    @Autowired(required = false)
    XUserDao xUserDao;
    @Autowired(required = false)
    TermDao termDao;


    @Override
    public List<Mark> findBySno(String sno) {
        System.out.println("sno="+sno);
        return this.markDao.findBySno(sno);
    }

    @Override
    public Mark findBySnoCno(String sno, String cno) {
        return null;
    }


    @Override
    public List<Mark> findByClznoCno(String clzno, String cno) {
        return this.markDao.findByClznoCno(clzno,cno);
    }

    @Override
    public int update(Mark mark) {
        return this.markDao.update(mark);
    }

    @Override
    public int add(Mark mark) {
        return this.markDao.add(mark);
    }

    @Override
    public List<MarkInfo> findByClzCno(String clzno, String cno, Integer termId) {
        List<MarkInfo> mks=new ArrayList<>();
        Clz clz = clzDao.findOne(clzno);
        System.out.println("班级"+clz);
        Course course = courseDao.findOne(cno);
        System.out.println("课程"+course);
        Term term = termDao.findById(termId);
        System.out.println(termId);
        System.out.println("学期"+term);
        List<XUser> stus = xUserDao.findByClz(clzno);
        MarkInfo mk;
        for (XUser xUser:stus) {
            mk=new MarkInfo();
            mk.getxUser().setUid(xUser.getUid());
            mk.getxUser().setSex(xUser.getSex());
            mk.getxUser().setGrade(xUser.getGrade());
            mk.getxUser().setGood(xUser.getGood());
            mk.getxUser().setPhone(xUser.getPhone());
            mk.getxUser().setUname(xUser.getUname());
            mk.getxUser().getClz().setClzno(clz.getClzno());
            mk.getxUser().getClz().setClzname(clz.getClzname());
            mk.getCourse().setCno(course.getCno());
            mk.getCourse().setCname(course.getCname());
            mk.getTerm().setTermId(term.getTermId());
            mk.getTerm().setTermName(term.getTermName());
            mk.setId(0);
            mk.setUsually(null);//平时分
            mk.setFraction(null);//考试分
            mk.setScore(0.0);//综合分
            Mark mark = markDao.findBySnoCno(xUser.getPhone(),cno,termId);
            System.out.println("成绩表里面的学生"+mark);
            if (mark!=null){
                mk.setUsually(mark.getUsually());
                mk.setFraction(mark.getFraction());
                mk.setScore(mark.getScore());
                mk.setId(mark.getId());
                mk.setTpost(mark.getTpost());
            }
            mks.add(mk);
        }
        return mks;
    }

    @Override
    public void doMark(List<Mark> marks) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String phone=(String) principal;
        System.out.println("教师="+phone);
        XUser tea = xUserDao.findByPhone(phone);
        System.out.println(tea);
        for (Mark mark:marks){
            XUser stu = xUserDao.findByPhone(mark.getxUser().getPhone());
            System.out.println("学生="+stu);
            if (mark.getUsually()!=null&&mark.getFraction()!=null){
                if (mark.getId()==0){
                    //计算平均分跟考试分
                    Double usu=mark.getUsually()*0.3;
                    Double fra=mark.getFraction()*0.7;
                    mark.setTpost(new Date());
                    mark.setScore(usu+fra);
                    mark.getxUser().setUid(tea.getUid());
                    markDao.add(mark);
                    Double avg = markDao.avg(mark.getxUser().getPhone(),mark.getTerm().getTermId());
                    System.out.println("avg="+avg);
                    //把学生成绩修改
                    stu.setGood(avg);
                    xUserDao.update(stu);
                }
                if (mark.getId()!=0){
                    Double usu=mark.getUsually()*0.3;
                    Double fra=mark.getFraction()*0.7;
                    mark.setTpost(new Date());
                    mark.setScore(usu+fra);
                    Double avg = markDao.avg(mark.getxUser().getPhone(), mark.getTerm().getTermId());
                    System.out.println("avg="+avg);
                    stu.setGood(avg);
                    markDao.update(mark);
                    xUserDao.update(stu);
                }
            }


        }
    }
    @Override
    public List<Mark> findTeacherScores(String sno) {
        // 这里需要根据实际情况实现获取教师评分数据的逻辑
        return markDao.findTeacherScores(sno);
    }

    @Override
    public void submitScores(List<Mark> scores) {
        // 这里需要根据实际情况实现提交评分的逻辑
        for (Mark score : scores) {
            markDao.updateScore(score);
        }
    }
}
